<?php
/**
 * Combo Bulletin Board
 *
 * =======================================================
 *
 * @package     Combo Bulletin Board
 * @author      Denis Wróbel
 * @license     http://www.gnu.org/licenses/gpl-3.0.html
 *
 * =======================================================
 *
 * $Date$
 * $Revision$
 * $Author$
 * $Test$
 * 
 */

if( !defined( 'IN_COMBO' ) || IN_COMBO == false )
{
	die( 'Go away...!' );
}

class Datbase
{
	/**
	 * Core class reference
	 * 
	 * @var object
	 */
	public $core					= NULL;
	
	/**
	 * Connection config
	 * 
	 * @var array
	 */
	private $config					= array();
	
	/**
	 * Connection handle
	 * 
	 * @var resource
	 */
	private $connection				= NULL;
	
	/**
	 * Queries count
	 * 
	 * @var integer
	 */
	private $queries_num			= 0;
	
	/**
	 * Queries list
	 * 
	 * @var Array
	 */
	private $queries_list			= array();
	
	/**
	 * Last query handle
	 * 
	 * @var resource
	 */
	private $last_query				= NULL;
	
	/**
	 * Constructor, connect with database
	 * 
	 * @param object $core   Core class object
	 * @param array  $config Array with connection info
	 */
	public function __construct( $core = NULL, $config = array() )
	{
		$dbselect = false;
		
		if( !$core instanceof ComboCore )
		{
			trigger_error( '<b>$core</b> is not instance of <b>ComboCore</b>.' );
		}
		
		$this->core   = $core;
		$this->config = $config;
		
		// Perform connection
		$this->connection = @mysql_connect( $config['host'], $config['user'], $config['pass'] );
		$dbselect		  = @mysql_select_db( $config['name'], $this->connection );
		
		if( !$this->connection || !$dbselect )
		{
			$this->core->triggerError( 'Database connection failure', array( 'Connection information can be invalid, check your configuration in <b>' . KERNEL . 'Config.php</b>.' ) );
		}
	}
	
	/**
	 * Returns connection handle
	 * 
	 * @return resource
	 */
	public function getConnection()
	{
		return $this->connection;
	}
	
	/**
	 * Get queries num
	 * 
	 * @return integer
	 */
	public function getQueriesNum()
	{
		return $this->queries_num;
	}
	
	/**
	 * Get queries list
	 * 
	 * @return array
	 */
	public function getQueriesList()
	{
		return $this->queries_list;
	}
	
	/**
	 * Get last query handle
	 * 
	 * @return resource
	 */
	public function getLastQuery()
	{
		return $this->last_query;
	}
	
	/**
	 * Execute query
	 * 
	 * @param string $sql SQL
	 * 
	 * @return resource Query handle
	 */
	public function query( $sql )
	{
		$start = microtime( true );
		$query = @mysql_query( $sql );
		$querying_time = ( microtime( true ) - $start );
		
		$this->queries_num++;
		$this->queries_list[]['sql'] = $sql;
		$this->queries_list[]['start'] = $start;
		$this->queries_list[]['querying_time'] = $querying_time;
		
		$this->last_query = $query;
		
		return $query;
	}
	
	/**
	 * Perform update query
	 *
	 * @param array  $data
	 * @param string $table     Table name
	 * @param string $condition Where condition
	 */
	public function update( $data, $table, $condition = '' )
	{
		$values = '';
		
		foreach( $data as $column => $value )
		{                       
			if( is_array( $value ) && $value[1] )
			{
				$values .= ', ' . $column . ' = ' . $value;
			}
			else
			{
				$values .= ', ' . $column . ' = \'' . $value . '\'';
			}                       
		}
		
		$this->query( 'UPDATE ' . DB_PREFIX . $table . ' SET ' . substr( $values, 2 ) . ( empty( $condition ) ? '' : ' WHERE ' . $condition ) );
	}
	
	/**
	 * Perform delete query
	 * @param string $table     Table name
	 * @param string $condition Where condition
	 */
	public function delete( $table, $condition = '' )
	{
		$this->query( 'DELETE FROM ' . DB_PREFIX . $table . ( empty( $condition ) ? '' : ' WHERE ' . $condition ) );
	}
	
	/**
	 * Perform insert query
	 *
	 * @param array  $data
	 * @param string $table     Table name
	 */
	public function insert( $data, $table )
	{
		$columns = '';
		$values = '';
		
		foreach( $data as $column => $value )
		{
			$columns .= ', ' . $column;
			$values .= ', \'' . $value . '\'';                      
		}
		
		$this->query( 'INSERT INTO ' . DB_PREFIX . $table . ' (' . substr( $columns, 2 ) . ') VALUES (' . substr( $values, 2 ) . ')' );
	}
	
	/**
	 * Fetch query as assoc array
	 * 
	 * @param resource $query Query handle
	 * 
	 * @return array
	 */
	public function fetchAssoc( $query = NULL )
	{
		if( !is_null( $query ) )
		{
			return mysql_fetch_assoc( $query );
		}
		else
		{
			return mysql_fetch_assoc( $this->last_query );
		}
	}
	
	/**
	 * Fetch query as array
	 * 
	 * @param resource $query Query handle
	 * 
	 * @return array
	 */
	public function fetchArray( $query = NULL )
	{
		if( !is_null( $query ) )
		{
			return mysql_fetch_array( $query );
		}
		else
		{
			return mysql_fetch_array( $this->last_query );
		}
	}
	
	/**
	 * Free memory from query
	 * 
	 * @param resource $query Query handle
	 */
	public function freeMemory( $query = NULL )
	{
		if( !is_null( $query ) )
		{
			return mysql_free_result( $query );
		}
		else
		{
			return mysql_free_result( $this->last_query );
		}
	}
	
	/**
	 * Escape string
	 * 
	 * @param string $string
	 * 
	 * @return string
	 */
	public function escape( $string )
	{
		$newString = $this->core->arrToStr( $string );
		
		return mysql_real_escape_string( $newString );
	}
	
	public function __destruct()
	{
		if( $this->connection )
		{
			mysql_close( $this->connection );
		}
	}
}

?>